package com.longdaji.decoration.ui.activitiesOfMine.collection;

import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.bean.Collection;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.data.network.HttpResponse;
import com.longdaji.decoration.utils.LogUtil;
import com.longdaji.decoration.utils.RxUtil;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Kanade on 2018/3/8 0008.
 */

public class CollectionPresenter extends RxPresenter<CollectionContract.View> implements CollectionContract.Presenter {
    private DataManager mDataManager;

    @Inject
    public CollectionPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
    }

    @Override
    public void initList(String userid) {
        doGetCollection(userid);

    }


    @Override
    public void doDeleteCollection(final String idList, final String userId) {
        LogUtil.d("collection test", idList + "--" + userId);

        addSubscribe(mDataManager.doDeleteCollectionApi(idList, userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                                   @Override
                                   public void onSuccess(HttpResponse httpResponse) {

                                       LogUtil.d("collection test", "succeed, doDeleteCollectionApi : " + idList + "    " + httpResponse.getMessage());
                                       doGetCollection(userId);
//                                       mView.showDeleteCollectionSucceed();
                                   }

                                   @Override
                                   public void onFailed(String msg) {
                                       LogUtil.d("collection test", "failed, doDeleteCollectionApi");

                                   }
                               }
                ));
    }

    @Override
    public void onFinishClick() {

    }

    @Override
    public void onDeleteClick() {

    }

    @Override
    public void onSelectClick() {

    }

    private void doGetCollection(String userid) {
        addSubscribe(mDataManager.doGetCollectionApi(userid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<Collection>handleResult())
                .subscribeWith(new CommonSubscriber<Collection>() {
                    @Override
                    public void onSuccess(Collection collection) {
                        LogUtil.d("succeed  doGetCollectionApi -- " + collection.toString());
                        mView.showGetCollectionSucceed(collection.getCollectionGoodsList());
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d("failed  doGetCollectionApi -- " + msg);
                    }
                }));
    }


}
